import {
  Controller,
  Get,
  Post,
  Query,
  UploadedFile,
  UseGuards,
  UseInterceptors,
  UsePipes,
  ValidationPipe,
} from '@nestjs/common';
import { DocumentService } from '../service/document.service';
import { DocumentTypeDto } from '../dto/document/document.dto';
import { JwtGuard } from '../guards/jwt.guard';
import {
  ApiTags,
  ApiOperation,
  ApiResponse,
  ApiConsumes,
  ApiBody,
} from '@nestjs/swagger';
import { FileInterceptor } from '@nestjs/platform-express';
import { FileObject, FileUploadDto } from '../dto/upload/file.dto';

@ApiTags('文档')
@Controller('document')
@UseGuards(JwtGuard)
@UsePipes(new ValidationPipe({ transform: true }))
export class DocumentController {
  constructor(private documentService: DocumentService) {}

  @ApiOperation({ summary: '获取文档内容' })
  @ApiResponse({ status: 200, description: '成功返回文档内容' })
  @ApiResponse({ status: 404, description: '文档不存在' })
  @Get()
  async getMarkdownDocument(@Query() dto: DocumentTypeDto) {
    const res = await this.documentService.getMarkdownDocument(dto.type);
    return {
      status: 200,
      content: res,
    };
  }

  @ApiOperation({ summary: '上传文档' })
  @ApiResponse({ status: 200, description: '上传成功' })
  @Post('upload')
  @ApiConsumes('multipart/form-data')
  @ApiBody({
    description: '文档上传',
    type: FileUploadDto,
  })
  @UseInterceptors(FileInterceptor('document'))
  async upload(@UploadedFile() file) {
    return {
      status: 200,
      message: '上传成功',
      data: {
        url: `/${(file as FileObject).filename}`,
      },
    };
  }
}
